package com.xiaobai.controller;

import com.alibaba.fastjson.JSON;
import com.xiaobai.common.ServiceResponse;
import com.xiaobai.dao.StudentCourseDao;
import com.xiaobai.pojo.StudentCourse;
import com.xiaobai.service.UnitsCourseService;
import com.xiaobai.service.course.CourseServiceImpl;
import com.xiaobai.service.course.ICourseService;
import com.xiaobai.vo.AllCourseAllList;
import com.xiaobai.vo.AllCourseWeekList;
import com.xiaobai.vo.CourseItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/course")
@Slf4j
public class CourseController {

    /**
     * 让我们想一想:jesessionId存在哪里呢?我们存到redis中设置失效时间30min
     * 存入格式: K->select:学号  V->jessionid  如果redis中查询不到,就调用登录方法(User里面的)
     */

    @Autowired
    private ICourseService courseService;

    @Autowired
    private CourseServiceImpl courseServiceImpl;

    @Autowired
    private UnitsCourseService unitsCourseService;

    @Autowired
    private StudentCourseDao studentCourseDao;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 更新当前学期课表
     * @param student_id
     * @param jsessionid
     * @return
     */
    @GetMapping("/selectNewCourse")
    public ServiceResponse selectNewCourse(String student_id,String jsessionid){

        /**
         * 去redis中查询是否有当前学生的课表
         */

//        String redisValue=stringRedisTemplate.opsForValue().get("course:"+student_id);
//        if(redisValue!=null){
//            //不给查,直接返回
//            return ServiceResponse.createByErrorMsg("时间未到");
//        }

        //每过七天才能1查一次
//        stringRedisTemplate.opsForValue().set("course:"+student_id,"1",7, TimeUnit.DAYS);

        Calendar calendar = Calendar.getInstance();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        if (hour >= 23 || hour <= 6) {
            return ServiceResponse.fail("教务网休息了");
        }


        AllCourseAllList allCourseAllList=new AllCourseAllList();

        String str=courseServiceImpl.getCourseAll(jsessionid);//查询出一学期的数据,zc为空即可

        if(str.length()<100){
            //8.27更新,在学期更换期间周课表更新慢,用这个上课任务查,里面都会插入
            AllCourseAllList allCourseByClassTask =
                    courseService.getAllCourseByClassTask(student_id, jsessionid);
            //微信更新周期7天  会导致DB重复插入 插入会覆盖的请放心

            return ServiceResponse.createBySuccess(allCourseByClassTask);
        }

        int startIndex=1;

        int endIndex=str.indexOf(",[]]");

        if (endIndex == -1) {
            // 跑到了这里, 那就是密码错误
            log.error("更新课表时截取字符串错误");
            log.error(str);
        }

        String resultJSON=str.substring(startIndex,endIndex);

        List<String> list=JSON.parseArray(resultJSON,String.class);

        List<CourseItem> courseItemList=new ArrayList<>();

        list.forEach(js->{
            CourseItem courseItem= JSON.parseObject(js,CourseItem.class);
            courseItemList.add(courseItem);//放入所有的课表对象
        });

        courseItemList.forEach(cs->{
            //根据(zc-1)+getIndexItem放入对应的周且下标也对应,记得可能要多送一节

            Integer zc=Integer.parseInt(cs.getZc());

            switch (zc){
                case 1:
                    allCourseAllList.getOneCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getOneCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 2:
                    allCourseAllList.getTwoCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getTwoCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 3:
                    allCourseAllList.getThreeCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getThreeCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 4:
                    allCourseAllList.getFourCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getFourCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 5:
                    allCourseAllList.getFiveCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getFiveCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 6:
                    allCourseAllList.getSixCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getSixCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 7:
                    allCourseAllList.getSevenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getSevenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 8:
                    allCourseAllList.getEigCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getEigCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 9:
                    allCourseAllList.getNineCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getNineCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 10:
                    allCourseAllList.getTenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getTenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 11:
                    allCourseAllList.getElevenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getElevenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 12:
                    allCourseAllList.getTwelveCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getTwelveCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 13:
                    allCourseAllList.getThirteenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getThirteenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 14:
                    allCourseAllList.getFourteenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getFourteenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 15:
                    allCourseAllList.getFifteenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getFifteenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 16:
                    allCourseAllList.getSixteenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getSixteenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 17:
                    allCourseAllList.getSeventeenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getSeventeenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 18:
                    allCourseAllList.getEighteenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getEighteenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 19:
                    allCourseAllList.getNineteenCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getNineteenCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
                case 20:
                    allCourseAllList.getTwentyCourseWeekList()
                            .getIndexCourseList()
                            .set(cs.getIndexItem(),cs);//放入对应周及其对应下标

                    if(cs.getIsContinuity()){
                        //如果是连续课,并且后面连课不是11,12节就多加一节课给周里面
                        allCourseAllList.getTwentyCourseWeekList()
                                .getIndexCourseList()
                                .set(cs.getIndexItem()+1,cs);//连课后面那节下标+1
                    }
                    break;
            }
        });

        /**
         * 新的课表对象已经查好了,接下来开始替换
         * 1.存入备份库
         * 2.查询自定义课表的数据
         * 3.替换(注意有备份的不要覆盖)
         */

        //update备份库
        unitsCourseService.updateCourse2(allCourseAllList,student_id);

        //下面更新另一个自定义库



        //这里批量查出数据
        List<StudentCourse> listDB=studentCourseDao.getAllCourseListByStudentId(student_id);
        //查出的课表,从小周到大周

        if(listDB.size()==0||listDB.get(0)==null||listDB.get(0).getConnect()==null||listDB.get(0).getConnect().equals("")){
            //记录8.22的错误,如果用户微信已经有了缓存,那么它只会更新课表,就会走到这里
            //而查出的DB中为空,所以现在就直接把数据给存入自定义库
            unitsCourseService.updateCourse3(allCourseAllList,student_id);
            return ServiceResponse.createBySuccess(allCourseAllList);
        }

        for(int i=1;i<=20;i++){


            switch (i){
                case 1:
                    /**
                     * 1.查出对应周
                     * 2.周转对象List
                     * 3.对象List遍历存好Map  k->index  v->备注内容
                     */
                    /**
                     * 必须要从库中查,因为我们需要自定义表中的数据!!!!!!避免覆盖掉备注
                     * studentCourseDao.selectWeekByWeekAndStudentId(student_id,1);
                     */
                    String week1=listDB.get(0).getConnect();
                    List<String> list1=JSON.parseArray(week1,String.class);

                    Map<Integer,CourseItem> map1=new HashMap();
                    for(int j=0;j<list1.size();j++){
                        CourseItem courseItem=JSON.parseObject(list1.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map1.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList1=new ArrayList<>();
                    for(Integer op:map1.keySet()){
                        integerList1.add(op);
                    }
                    for(int j=0;j<integerList1.size();j++){

                        if(allCourseAllList.getOneCourseWeekList()
                                .getIndexCourseList().get(integerList1.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            /**
                             * 直接把对象赋给自定义的,毕竟官方的课都为空了
                             */
                            allCourseAllList.getOneCourseWeekList()
                                    .getIndexCourseList().set(integerList1.get(j),map1.get(integerList1.get(j)));
                        }else {
                            /**
                             * 官方的课不为空,我们就仅把备注留下来即可
                             */
                            allCourseAllList.getOneCourseWeekList()
                                    .getIndexCourseList().get(integerList1.get(j))
                                    .setRemarks(map1.get(integerList1.get(j)).getRemarks());
                        }
                    }
                    break;
                case 2:
                    String week2=listDB.get(1).getConnect();
                    List<String> list2=JSON.parseArray(week2,String.class);

                    Map<Integer,CourseItem> map2=new HashMap();
                    for(int j=0;j<list2.size();j++){
                        CourseItem courseItem=JSON.parseObject(list2.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map2.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList2=new ArrayList<>();
                    for(Integer op:map2.keySet()){
                        integerList2.add(op);
                    }
                    for(int j=0;j<integerList2.size();j++){
                        if(allCourseAllList.getTwoCourseWeekList()
                                .getIndexCourseList().get(integerList2.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getTwoCourseWeekList()
                                    .getIndexCourseList().set(integerList2.get(j),map2.get(integerList2.get(j)));
                        }else {
                            allCourseAllList.getTwoCourseWeekList()
                                    .getIndexCourseList().get(integerList2.get(j))
                                    .setRemarks(map2.get(integerList2.get(j)).getRemarks());
                        }
                    }
                    break;
                case 3:
                    String week3=listDB.get(2).getConnect();
                    List<String> list3=JSON.parseArray(week3,String.class);

                    Map<Integer,CourseItem> map3=new HashMap();
                    for(int j=0;j<list3.size();j++){
                        CourseItem courseItem=JSON.parseObject(list3.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map3.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList3=new ArrayList<>();
                    for(Integer op:map3.keySet()){
                        integerList3.add(op);
                    }
                    for(int j=0;j<integerList3.size();j++){
                        if(allCourseAllList.getThreeCourseWeekList()
                                .getIndexCourseList().get(integerList3.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getThreeCourseWeekList()
                                    .getIndexCourseList().set(integerList3.get(j),map3.get(integerList3.get(j)));
                        }else {
                            allCourseAllList.getThreeCourseWeekList()
                                    .getIndexCourseList().get(integerList3.get(j))
                                    .setRemarks(map3.get(integerList3.get(j)).getRemarks());
                        }
                    }
                    break;
                case 4:
                    String week4=listDB.get(3).getConnect();
                    List<String> list4=JSON.parseArray(week4,String.class);

                    Map<Integer,CourseItem> map4=new HashMap();
                    for(int j=0;j<list4.size();j++){
                        CourseItem courseItem=JSON.parseObject(list4.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map4.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList4=new ArrayList<>();
                    for(Integer op:map4.keySet()){
                        integerList4.add(op);
                    }
                    for(int j=0;j<integerList4.size();j++){
                        if(allCourseAllList.getFourCourseWeekList()
                                .getIndexCourseList().get(integerList4.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getFourCourseWeekList()
                                    .getIndexCourseList().set(integerList4.get(j),map4.get(integerList4.get(j)));
                        }else {
                            allCourseAllList.getFourCourseWeekList()
                                    .getIndexCourseList().get(integerList4.get(j))
                                    .setRemarks(map4.get(integerList4.get(j)).getRemarks());
                        }
                    }
                    break;
                case 5:
                    String week5=listDB.get(4).getConnect();
                    List<String> list5=JSON.parseArray(week5,String.class);

                    Map<Integer,CourseItem> map5=new HashMap();
                    for(int j=0;j<list5.size();j++){
                        CourseItem courseItem=JSON.parseObject(list5.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map5.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList5=new ArrayList<>();
                    for(Integer op:map5.keySet()){
                        integerList5.add(op);
                    }
                    for(int j=0;j<integerList5.size();j++){
                        if(allCourseAllList.getFiveCourseWeekList()
                                .getIndexCourseList().get(integerList5.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getFiveCourseWeekList()
                                    .getIndexCourseList().set(integerList5.get(j),map5.get(integerList5.get(j)));
                        }else {
                            allCourseAllList.getFiveCourseWeekList()
                                    .getIndexCourseList().get(integerList5.get(j))
                                    .setRemarks(map5.get(integerList5.get(j)).getRemarks());
                        }
                    }
                    break;
                case 6:
                    String week6=listDB.get(5).getConnect();
                    List<String> list6=JSON.parseArray(week6,String.class);

                    Map<Integer,CourseItem> map6=new HashMap();
                    for(int j=0;j<list6.size();j++){
                        CourseItem courseItem=JSON.parseObject(list6.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map6.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList6=new ArrayList<>();
                    for(Integer op:map6.keySet()){
                        integerList6.add(op);
                    }
                    for(int j=0;j<integerList6.size();j++){
                        if(allCourseAllList.getSixCourseWeekList()
                                .getIndexCourseList().get(integerList6.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getSixCourseWeekList()
                                    .getIndexCourseList().set(integerList6.get(j),map6.get(integerList6.get(j)));
                        }else {
                            allCourseAllList.getSixCourseWeekList()
                                    .getIndexCourseList().get(integerList6.get(j))
                                    .setRemarks(map6.get(integerList6.get(j)).getRemarks());
                        }
                    }
                    break;
                case 7:
                    String week7=listDB.get(6).getConnect();
                    List<String> list7=JSON.parseArray(week7,String.class);

                    Map<Integer,CourseItem> map7=new HashMap();
                    for(int j=0;j<list7.size();j++){
                        CourseItem courseItem=JSON.parseObject(list7.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map7.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList7=new ArrayList<>();
                    for(Integer op:map7.keySet()){
                        integerList7.add(op);
                    }
                    for(int j=0;j<integerList7.size();j++){
                        if(allCourseAllList.getSevenCourseWeekList()
                                .getIndexCourseList().get(integerList7.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getSevenCourseWeekList()
                                    .getIndexCourseList().set(integerList7.get(j),map7.get(integerList7.get(j)));
                        }else {
                            allCourseAllList.getSevenCourseWeekList()
                                    .getIndexCourseList().get(integerList7.get(j))
                                    .setRemarks(map7.get(integerList7.get(j)).getRemarks());
                        }
                    }
                    break;
                case 8:
                    String week8=listDB.get(7).getConnect();
                    List<String> list8=JSON.parseArray(week8,String.class);

                    Map<Integer,CourseItem> map8=new HashMap();
                    for(int j=0;j<list8.size();j++){
                        CourseItem courseItem=JSON.parseObject(list8.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map8.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList8=new ArrayList<>();
                    for(Integer op:map8.keySet()){
                        integerList8.add(op);
                    }
                    for(int j=0;j<integerList8.size();j++){
                        if(allCourseAllList.getEigCourseWeekList()
                                .getIndexCourseList().get(integerList8.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getEigCourseWeekList()
                                    .getIndexCourseList().set(integerList8.get(j),map8.get(integerList8.get(j)));
                        }else {
                            allCourseAllList.getEigCourseWeekList()
                                    .getIndexCourseList().get(integerList8.get(j))
                                    .setRemarks(map8.get(integerList8.get(j)).getRemarks());
                        }
                    }
                    break;
                case 9:
                    String week9=listDB.get(8).getConnect();
                    List<String> list9=JSON.parseArray(week9,String.class);

                    Map<Integer,CourseItem> map9=new HashMap();
                    for(int j=0;j<list9.size();j++){
                        CourseItem courseItem=JSON.parseObject(list9.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map9.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList9=new ArrayList<>();
                    for(Integer op:map9.keySet()){
                        integerList9.add(op);
                    }
                    for(int j=0;j<integerList9.size();j++){
                        if(allCourseAllList.getNineCourseWeekList()
                                .getIndexCourseList().get(integerList9.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getNineCourseWeekList()
                                    .getIndexCourseList().set(integerList9.get(j),map9.get(integerList9.get(j)));
                        }else {
                            allCourseAllList.getNineCourseWeekList()
                                    .getIndexCourseList().get(integerList9.get(j))
                                    .setRemarks(map9.get(integerList9.get(j)).getRemarks());
                        }
                    }
                    break;
                case 10:
                    String week10=listDB.get(9).getConnect();
                    List<String> list10=JSON.parseArray(week10,String.class);

                    Map<Integer,CourseItem> map10=new HashMap();
                    for(int j=0;j<list10.size();j++){
                        CourseItem courseItem=JSON.parseObject(list10.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map10.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList10=new ArrayList<>();
                    for(Integer op:map10.keySet()){
                        integerList10.add(op);
                    }
                    for(int j=0;j<integerList10.size();j++){
                        if(allCourseAllList.getTenCourseWeekList()
                                .getIndexCourseList().get(integerList10.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getTenCourseWeekList()
                                    .getIndexCourseList().set(integerList10.get(j),map10.get(integerList10.get(j)));
                        }else {
                            allCourseAllList.getTenCourseWeekList()
                                    .getIndexCourseList().get(integerList10.get(j))
                                    .setRemarks(map10.get(integerList10.get(j)).getRemarks());
                        }
                    }
                    break;
                case 11:
                    String week11=listDB.get(10).getConnect();
                    List<String> list11=JSON.parseArray(week11,String.class);

                    Map<Integer,CourseItem> map11=new HashMap();
                    for(int j=0;j<list11.size();j++){
                        CourseItem courseItem=JSON.parseObject(list11.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map11.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList11=new ArrayList<>();
                    for(Integer op:map11.keySet()){
                        integerList11.add(op);
                    }
                    for(int j=0;j<integerList11.size();j++){
                        if(allCourseAllList.getElevenCourseWeekList()
                                .getIndexCourseList().get(integerList11.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getElevenCourseWeekList()
                                    .getIndexCourseList().set(integerList11.get(j),map11.get(integerList11.get(j)));
                        }else {
                            allCourseAllList.getElevenCourseWeekList()
                                    .getIndexCourseList().get(integerList11.get(j))
                                    .setRemarks(map11.get(integerList11.get(j)).getRemarks());
                        }
                    }
                    break;
                case 12:
                    String week12=listDB.get(11).getConnect();
                    List<String> list12=JSON.parseArray(week12,String.class);

                    Map<Integer,CourseItem> map12=new HashMap();
                    for(int j=0;j<list12.size();j++){
                        CourseItem courseItem=JSON.parseObject(list12.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map12.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList12=new ArrayList<>();
                    for(Integer op:map12.keySet()){
                        integerList12.add(op);
                    }
                    for(int j=0;j<integerList12.size();j++){
                        if(allCourseAllList.getTwelveCourseWeekList()
                                .getIndexCourseList().get(integerList12.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getTwelveCourseWeekList()
                                    .getIndexCourseList().set(integerList12.get(j),map12.get(integerList12.get(j)));
                        }else {
                            allCourseAllList.getTwelveCourseWeekList()
                                    .getIndexCourseList().get(integerList12.get(j))
                                    .setRemarks(map12.get(integerList12.get(j)).getRemarks());
                        }
                    }
                    break;
                case 13:
                    String week13=listDB.get(12).getConnect();
                    List<String> list13=JSON.parseArray(week13,String.class);

                    Map<Integer,CourseItem> map13=new HashMap();
                    for(int j=0;j<list13.size();j++){
                        CourseItem courseItem=JSON.parseObject(list13.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map13.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList13=new ArrayList<>();
                    for(Integer op:map13.keySet()){
                        integerList13.add(op);
                    }
                    for(int j=0;j<integerList13.size();j++){
                        if(allCourseAllList.getThirteenCourseWeekList()
                                .getIndexCourseList().get(integerList13.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getThirteenCourseWeekList()
                                    .getIndexCourseList().set(integerList13.get(j),map13.get(integerList13.get(j)));
                        }else {
                            allCourseAllList.getThirteenCourseWeekList()
                                    .getIndexCourseList().get(integerList13.get(j))
                                    .setRemarks(map13.get(integerList13.get(j)).getRemarks());
                        }
                    }
                    break;
                case 14:
                    String week14=listDB.get(13).getConnect();
                    List<String> list14=JSON.parseArray(week14,String.class);

                    Map<Integer,CourseItem> map14=new HashMap();
                    for(int j=0;j<list14.size();j++){
                        CourseItem courseItem=JSON.parseObject(list14.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map14.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList14=new ArrayList<>();
                    for(Integer op:map14.keySet()){
                        integerList14.add(op);
                    }
                    for(int j=0;j<integerList14.size();j++){
                        if(allCourseAllList.getFourteenCourseWeekList()
                                .getIndexCourseList().get(integerList14.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getFourteenCourseWeekList()
                                    .getIndexCourseList().set(integerList14.get(j),map14.get(integerList14.get(j)));
                        }else {
                            allCourseAllList.getFourteenCourseWeekList()
                                    .getIndexCourseList().get(integerList14.get(j))
                                    .setRemarks(map14.get(integerList14.get(j)).getRemarks());
                        }
                    }
                    break;
                case 15:
                    String week15=listDB.get(14).getConnect();
                    List<String> list15=JSON.parseArray(week15,String.class);

                    Map<Integer,CourseItem> map15=new HashMap();
                    for(int j=0;j<list15.size();j++){
                        CourseItem courseItem=JSON.parseObject(list15.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map15.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList15=new ArrayList<>();
                    for(Integer op:map15.keySet()){
                        integerList15.add(op);
                    }
                    for(int j=0;j<integerList15.size();j++){
                        if(allCourseAllList.getFifteenCourseWeekList()
                                .getIndexCourseList().get(integerList15.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getFifteenCourseWeekList()
                                    .getIndexCourseList().set(integerList15.get(j),map15.get(integerList15.get(j)));
                        }else {
                            allCourseAllList.getFifteenCourseWeekList()
                                    .getIndexCourseList().get(integerList15.get(j))
                                    .setRemarks(map15.get(integerList15.get(j)).getRemarks());
                        }
                    }
                    break;
                case 16:
                    String week16=listDB.get(15).getConnect();
                    List<String> list16=JSON.parseArray(week16,String.class);

                    Map<Integer,CourseItem> map16=new HashMap();
                    for(int j=0;j<list16.size();j++){
                        CourseItem courseItem=JSON.parseObject(list16.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map16.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList16=new ArrayList<>();
                    for(Integer op:map16.keySet()){
                        integerList16.add(op);
                    }
                    for(int j=0;j<integerList16.size();j++){
                        if(allCourseAllList.getSixteenCourseWeekList()
                                .getIndexCourseList().get(integerList16.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getSixteenCourseWeekList()
                                    .getIndexCourseList().set(integerList16.get(j),map16.get(integerList16.get(j)));
                        }else {
                            allCourseAllList.getSixteenCourseWeekList()
                                    .getIndexCourseList().get(integerList16.get(j))
                                    .setRemarks(map16.get(integerList16.get(j)).getRemarks());
                        }
                    }
                    break;
                case 17:
                    String week17=listDB.get(16).getConnect();
                    List<String> list17=JSON.parseArray(week17,String.class);

                    Map<Integer,CourseItem> map17=new HashMap();
                    for(int j=0;j<list17.size();j++){
                        CourseItem courseItem=JSON.parseObject(list17.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map17.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList17=new ArrayList<>();
                    for(Integer op:map17.keySet()){
                        integerList17.add(op);
                    }
                    for(int j=0;j<integerList17.size();j++){
                        if(allCourseAllList.getSeventeenCourseWeekList()
                                .getIndexCourseList().get(integerList17.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getSeventeenCourseWeekList()
                                    .getIndexCourseList().set(integerList17.get(j),map17.get(integerList17.get(j)));
                        }else {
                            allCourseAllList.getSeventeenCourseWeekList()
                                    .getIndexCourseList().get(integerList17.get(j))
                                    .setRemarks(map17.get(integerList17.get(j)).getRemarks());
                        }
                    }
                    break;
                case 18:
                    String week18=listDB.get(17).getConnect();
                    List<String> list18=JSON.parseArray(week18,String.class);

                    Map<Integer,CourseItem> map18=new HashMap();
                    for(int j=0;j<list18.size();j++){
                        CourseItem courseItem=JSON.parseObject(list18.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map18.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList18=new ArrayList<>();
                    for(Integer op:map18.keySet()){
                        integerList18.add(op);
                    }
                    for(int j=0;j<integerList18.size();j++){
                        if(allCourseAllList.getEighteenCourseWeekList()
                                .getIndexCourseList().get(integerList18.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getEighteenCourseWeekList()
                                    .getIndexCourseList().set(integerList18.get(j),map18.get(integerList18.get(j)));
                        }else {
                            allCourseAllList.getEighteenCourseWeekList()
                                    .getIndexCourseList().get(integerList18.get(j))
                                    .setRemarks(map18.get(integerList18.get(j)).getRemarks());
                        }
                    }
                    break;
                case 19:
                    String week19=listDB.get(18).getConnect();
                    List<String> list19=JSON.parseArray(week19,String.class);

                    Map<Integer,CourseItem> map19=new HashMap();
                    for(int j=0;j<list19.size();j++){
                        CourseItem courseItem=JSON.parseObject(list19.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map19.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList19=new ArrayList<>();
                    for(Integer op:map19.keySet()){
                        integerList19.add(op);
                    }
                    for(int j=0;j<integerList19.size();j++){
                        if(allCourseAllList.getNineteenCourseWeekList()
                                .getIndexCourseList().get(integerList19.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getNineteenCourseWeekList()
                                    .getIndexCourseList().set(integerList19.get(j),map19.get(integerList19.get(j)));
                        }else {
                            allCourseAllList.getNineteenCourseWeekList()
                                    .getIndexCourseList().get(integerList19.get(j))
                                    .setRemarks(map19.get(integerList19.get(j)).getRemarks());
                        }
                    }
                    break;
                case 20:
                    String week20=listDB.get(19).getConnect();
                    List<String> list20=JSON.parseArray(week20,String.class);

                    Map<Integer,CourseItem> map20=new HashMap();
                    for(int j=0;j<list20.size();j++){
                        CourseItem courseItem=JSON.parseObject(list20.get(j),CourseItem.class);
                        if(courseItem!=null&&courseItem.getRemarks()!=null){
                            map20.put(j,courseItem);
                        }
                    }
                    List<Integer> integerList20=new ArrayList<>();
                    for(Integer op:map20.keySet()){
                        integerList20.add(op);
                    }
                    for(int j=0;j<integerList20.size();j++){
                        if(allCourseAllList.getTwentyCourseWeekList()
                                .getIndexCourseList().get(integerList20.get(j))==null){
                            //官方给定课为空,用户备注写在了无课的节次
                            allCourseAllList.getTwentyCourseWeekList()
                                    .getIndexCourseList().set(integerList20.get(j),map20.get(integerList20.get(j)));
                        }else {
                            allCourseAllList.getTwentyCourseWeekList()
                                    .getIndexCourseList().get(integerList20.get(j))
                                    .setRemarks(map20.get(integerList20.get(j)).getRemarks());
                        }
                    }
                    break;
            }
        }

        unitsCourseService.updateCourse3(allCourseAllList,student_id);

        /**
         * 注意事项
         * 1.获取当前学期数据后应该先更新备份表,然后再更新自定义表
         * 2.不要把自定义表的备份清理了
         */

        return ServiceResponse.createBySuccess(allCourseAllList);
    }

    /**
     * 用户修改了某一周的课表数据,此处接收并且传入自定义库
     */
    @PostMapping("/updateWeekCourse")
    public ServiceResponse updateWeekCourse(String student_id,String week,String weekCourseJSON){

//        String redisValue=stringRedisTemplate.opsForValue().get("security:"+student_id);
//
//        String security[]=redisValue.split("@");
//
//        if(!StringUtils.equals(security[1],uuid)){
//            //如果不相等
//            return ServiceResponse.createByErrorMsg("禁止横向越权");
//        }

        courseService.updateWeekCourse(student_id,week,weekCourseJSON);

        return ServiceResponse.createBySuccess();
    }

    /**
     * 用户点击了重置当前周课程表
     */
    @PostMapping("/resetWeekCourse")
    public ServiceResponse resetWeekCourse(String student_id,String week){

        /**
         * 注意重置就是会默认清理掉备注(新增+备注)
         * 1.从备份表中查出对应的周
         * 2.自定义表转为备份表数据
         * 3.返回备份表查出的数据
         */

        //查出了备份表中的数据
        String basicJSON=studentCourseDao.selectWeekFormBasicCourse(student_id,Integer.parseInt(week));

        StudentCourse studentCourse=new StudentCourse();
        studentCourse.setStudentId(student_id);
        studentCourse.setWeek(Integer.parseInt(week));
        studentCourse.setConnect(basicJSON);
        //在去修改自定义表
        studentCourseDao.updateByStudentIdZidingyi(studentCourse);

        List<String> list=JSON.parseArray(basicJSON,String.class);

        List<CourseItem> courseItemList=new ArrayList<>();

        for(int i=0;i<list.size();i++){
            CourseItem courseItem=JSON.parseObject(list.get(i),CourseItem.class);
            courseItemList.add(courseItem);
        }

        //再传回给前端备份表中的数据
        switch (Integer.parseInt(week)){
            case 1:
            AllCourseWeekList oneCourseWeekList=new AllCourseWeekList();
                oneCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(oneCourseWeekList);

            case 2:
            AllCourseWeekList twoCourseWeekList=new AllCourseWeekList();
            twoCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(twoCourseWeekList);

            case 3:
            AllCourseWeekList threeCourseWeekList=new AllCourseWeekList();
                threeCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(threeCourseWeekList);

            case 4:
            AllCourseWeekList fourCourseWeekList=new AllCourseWeekList();
                fourCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(fourCourseWeekList);

            case 5:
            AllCourseWeekList fiveCourseWeekList=new AllCourseWeekList();
                fiveCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(fiveCourseWeekList);

            case 6:
            AllCourseWeekList sixCourseWeekList=new AllCourseWeekList();
                sixCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(sixCourseWeekList);

            case 7:
            AllCourseWeekList sevenCourseWeekList=new AllCourseWeekList();
                sevenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(sevenCourseWeekList);

            case 8:
            AllCourseWeekList eigCourseWeekList=new AllCourseWeekList();
                eigCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(eigCourseWeekList);

            case 9:
            AllCourseWeekList nineCourseWeekList=new AllCourseWeekList();
                nineCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(nineCourseWeekList);

            case 10:
            AllCourseWeekList tenCourseWeekList=new AllCourseWeekList();
                tenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(tenCourseWeekList);

            case 11:
            AllCourseWeekList elevenCourseWeekList=new AllCourseWeekList();
                elevenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(elevenCourseWeekList);

            case 12:
            AllCourseWeekList twelveCourseWeekList=new AllCourseWeekList();
                twelveCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(twelveCourseWeekList);

            case 13:
            AllCourseWeekList thirteenCourseWeekList=new AllCourseWeekList();
                thirteenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(thirteenCourseWeekList);

            case 14:
            AllCourseWeekList fourteenCourseWeekList=new AllCourseWeekList();
                fourteenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(fourteenCourseWeekList);

            case 15:
            AllCourseWeekList fifteenCourseWeekList=new AllCourseWeekList();
                fifteenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(fifteenCourseWeekList);

            case 16:
            AllCourseWeekList sixteenCourseWeekList=new AllCourseWeekList();
                sixteenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(sixteenCourseWeekList);

            case 17:
            AllCourseWeekList seventeenCourseWeekList=new AllCourseWeekList();
                seventeenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(seventeenCourseWeekList);

            case 18:
            AllCourseWeekList eighteenCourseWeekList=new AllCourseWeekList();
                eighteenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(eighteenCourseWeekList);

            case 19:
            AllCourseWeekList nineteenCourseWeekList=new AllCourseWeekList();
                nineteenCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(nineteenCourseWeekList);

            case 20:
            AllCourseWeekList twentyCourseWeekList=new AllCourseWeekList();
                twentyCourseWeekList.setIndexCourseList(courseItemList);
                return ServiceResponse.createBySuccess(twentyCourseWeekList);
        }

        throw new RuntimeException("不该走到这里的啊~");
    }

}
